﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：UnitManage.ascx.cs
//
//文件功能描述： 计量单位管理
// 
//创建标识：2007.11.22 王林进
//创建描述：
//
------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using localhost;

public partial class UserControls_UnitManage : System.Web.UI.UserControl
{
    DataSet dataSet = new DataSet();
    //实例化一个服务
    ProductService unitManage = new ProductService();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            //dataSet = unitManage.GetUnit();

            //this.GridviewBind(dataSet);

            int iPageSize =10;
            int iPageIndex=1;

            dataSet = unitManage.GetUnitForPaging(iPageSize, iPageIndex);

            this.GridViewBind(dataSet);

            hdfUnitRows.Value = (unitManage.GetUnitRows()).ToString();

            Paging(iPageSize, iPageIndex);

           

            
        }

        #region
        //string deleteMsg = "删除操作不可恢复,你确定吗?";
        //string updateMsg = "是否确定修改计量单位?";
        //this.ibtnDelete.Attributes.Add("onClick", "return confirm('" + deleteMsg + "')");
        //this.ibtnSave.Attributes.Add("onClick", "return confirm('" + updateMsg + "')");
        #endregion

    }

    /// <summary>
    /// 点击按钮处理事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
    {   
        //初始化布尔类型的变量
        bool result = false;

        try
        {   //获得一个Unit实体
            localhost.Unit unitAdd = new localhost.Unit();
            //判断添加的计量单位是否为空
            if (this.txtAddUnit.Text != string.Empty)
            {
                //不为空时就添加
                unitAdd.Name = txtAddUnit.Text.Trim();
                //获得添加UnitID的结果,true表示成功,false表示失败
                result = unitManage.AddUnit(unitAdd);
            }
            if (result == true)
            {
                this.lblAddResult.Text = "添加成功";
                //重新绑定数据
                //dataSet = unitManage.GetUnit();
                //this.GridviewBind(dataSet);
                int iPageSize = 10;
                int iPageIndex = 1;

                dataSet = unitManage.GetUnitForPaging(iPageSize, iPageIndex);

                this.GridViewBind(dataSet);

                hdfUnitRows.Value = (unitManage.GetUnitRows()).ToString();

                Paging(iPageSize, iPageIndex);
            }
        }
        catch (Exception ex)
        {
            this.lblAddResult.Text = "添加失败";
        }

    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="ds">实例一个数据集</param>
    //private void GridviewBind(DataSet ds)
    //{
    //    try
    //    {
    //        this.gdvUnitManage.DataSource = ds.Tables[0].DefaultView;
    //        this.gdvUnitManage.DataBind();

    //        LblCurrentIndex.Text = "第 " + (this.gdvUnitManage.PageIndex + 1).ToString() + " 页";
    //        LblPageCount.Text = "共 " + gdvUnitManage.PageCount.ToString() + " 页";
    //        LblRecordCount.Text = "总共 " + ds.Tables[0].Rows.Count.ToString() + " 条";
    //        if (ds.Tables[0].Rows.Count == 0)
    //        {
    //            btnFirst.Visible = false;
    //            btnPrev.Visible = false;
    //            btnNext.Visible = false;
    //            btnLast.Visible = false;

    //            LblCurrentIndex.Visible = false;
    //            LblPageCount.Visible = false;
    //            LblRecordCount.Visible = false;

    //            LblNoRecord.Visible = true;
    //        }
    //        else if (gdvUnitManage.PageCount == 1)
    //        {
    //            btnFirst.Visible = false;
    //            btnPrev.Visible = false;
    //            btnNext.Visible = false;
    //            btnLast.Visible = false;
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        Response.Write("数据库错误，错误原因：" + ex.Message);
    //        Response.End();
    //    }
    //}

    private void GridViewBind(DataSet ds)
    {
        this.gdvUnitManage.DataSource = ds.Tables[0].DefaultView;
        this.gdvUnitManage.DataBind();
    }

    private void Paging(int iPageSize, int iPageIndex)
    {
        int iUnitRows = int.Parse(hdfUnitRows.Value.Trim());

        if (iUnitRows > 0)
        {
            lblRecordCount.Text = "共" + iUnitRows.ToString() + "条";

            lblNoRecord.Visible = false;
            lblPageCount.Visible = true;

            int iPages = iUnitRows / iPageSize;

            if (iUnitRows % iPageSize != 0)
            {
                iPages += 1;
            }

            lblPageCount.Text = "共" + iPages.ToString() + "页";

            if (iPages > 1)
            {
                btnFirst.Visible = true;
                btnLast.Visible = true;
                btnNext.Visible = true;
                btnPrev.Visible = true;
                //if (iPages == iPageIndex)
                //{
                //    btnLast.Enabled = false;
                //}
            }
            else
            {
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;
                btnFirst.Visible = false;
            }

            lblCurrentIndex.Text = "第" + iPageIndex.ToString() + "页";
            lblCurrentIndex.Visible = true;
        }

        else
        {
            lblNoRecord.Visible = true;
            lblCurrentIndex.Visible = false;
            lblPageCount.Visible = false;
            lblRecordCount.Visible = false;

            btnFirst.Visible = false;
            btnLast.Visible = false;
            btnNext.Visible = false;
            btnPrev.Visible = false;

        }
    }

    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        string strPageIndex = lblCurrentIndex.Text.Trim();
        strPageIndex = strPageIndex.Substring(1);
        strPageIndex = strPageIndex.Substring(0,strPageIndex.Length - strPageIndex.LastIndexOf(""));
        int iPageIndex = int.Parse(strPageIndex );

        string strPageCount = lblPageCount.Text.Trim();
        strPageCount = strPageCount.Substring(1);
        strPageCount = strPageCount.Substring(0,strPageCount.Length - strPageCount.LastIndexOf(""));
        int iPageCount = int.Parse(strPageCount);

        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            case "prev":
                if (iPageIndex > 0)
                {
                    iPageIndex -= 1;
                    if (iPageIndex == 0)
                    {
                        iPageIndex = 1;
                    }
                }
                break;
            case "next":
                if (iPageIndex < iPageCount)
                {
                    iPageIndex += 1;
                }
                break;
            case "last":
                iPageIndex = iPageCount;
                break;
            default:
                iPageIndex = 1;
                break;
        }
        int iPageSize = 10;
        this.GridViewBind (this.unitManage.GetUnitForPaging(iPageSize, iPageIndex));
        Paging(iPageSize, iPageIndex);
    }

    


    ///// <summary>
    ///// 删除所选的行
    ///// </summary>
    ///// <param name="sender"></param>
    ///// <param name="e"></param>
    //protected void gdvUnitManage_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{    //获得删除的行索引
    //    int iUnitID = Int32.Parse(gdvUnitManage.DataKeys[e.RowIndex].Value.ToString());
    //    //获得删除计量单位的结果,true表示成功,false表示失败
    //    bool result = unitManage.DeleteUnit(iUnitID);
    //    //重新绑定数据
    //    dataSet = unitManage.GetUnit();
    //    this.GridviewBind(dataSet);
    //}

    /// <summary>
    /// 修改计量单位的名称
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvUnitManage_RowCommand(object sender, GridViewCommandEventArgs e)
    {   //如果行命令的名称等于updatename就执行
        if (e.CommandName == "updateName")
        {
            //初始化返回结果result为false
            bool result = false;

            //把选择的行索引转化为整数 
            int index = Int32.Parse(e.CommandArgument.ToString());

            //获得一个Unit实体
            localhost.Unit unitUPdate = new localhost.Unit();

            //获得选择的行的计量单位的ID 
            string unitID = this.gdvUnitManage.DataKeys[index].Values[0].ToString();

            //获得选择的行的计量单位的名称
            string name = ((TextBox)this.gdvUnitManage.Rows[index].FindControl("txtUnitName")).Text.Trim();

            //把unitid转化为整数赋值unitupdate 
            unitUPdate.UnitID = Convert.ToInt32(unitID);

            //把name转化为整数赋值unitupdate 
            unitUPdate.Name = name;

            //获得修改UnitID的结果
            result = unitManage.UpdateUnit(unitUPdate);        
        }
    }
    protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
    {   
        //获得删除的行索引
        int iUnitID = Convert.ToInt32(((ImageButton)sender).CommandArgument.Trim());
        //int iUnitID = Int32.Parse(gdvUnitManage.DataKeys[e.RowIndex].Value.ToString());
        //获得删除计量单位的结果,true表示成功,false表示失败
        bool result = unitManage.DeleteUnit(iUnitID);
        //重新绑定数据
        int iPageSize = 10;
        int iPageIndex = 1;

        dataSet = unitManage.GetUnitForPaging(iPageSize, iPageIndex);

        this.GridViewBind(dataSet);

        hdfUnitRows.Value = (unitManage.GetUnitRows()).ToString();

        Paging(iPageSize, iPageIndex);
    }
}